/*
 * Copyright (c) 2009 LastFM.Desktop Anton Pavlik.
 * All Rights Reserved.
 * 
 * You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, 
 * with or without modifications, and in Source or Object form, provided that You meet the following 
 * conditions:
 * 1. You must give any other recipients of the Work or Derivative Works a copy of this License; and
 *
 * 2. You must cause any modified files to carry prominent notices stating that You changed the files; 
 * and
 *
 * 3. You must retain, in the Source form of any Derivative Works that You distribute, all copyright, 
 * patent, trademark, and attribution notices from the Source form of the Work, excluding those notices 
 * that do not pertain to any part of the Derivative Works.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * You may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, Licensor provides the Work 
 * (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
 * CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties 
 * or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. 
 * YOU ARE SOLELY RESPONSIBLE FOR DETERMINING THE APPROPRIATENESS OF USING OR REDISTRIBUTING THE WORK
 * AND ASSUME ANY RISKS ASSOCIATED WITH YOUR EXERCISE OF PERMISSIONS UNDER THIS LICENSE. 
 */
package org.shake.lastfm.ui.playlist;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import javax.swing.AbstractListModel;

import org.shake.lastfm.model.Track;
import org.shake.lastfm.model.TrackStatus;

public class TrackListModel extends AbstractListModel
{
    private static final long serialVersionUID = 1L;
    private final List<Track> data;
    
    public TrackListModel()
    {
        this.data = new ArrayList<Track>();
    }

    @Override
    public Track getElementAt(int index)
    {
        if (index != -1 && index < this.data.size())
        {
            return this.data.get(index);
        }
        else
        {
            return null;
        }
    }

    @Override
    public int getSize()
    {
        return this.data.size();
    }
    
    public void setData(Collection<Track> newData)
    {
        this.data.clear();
        this.data.addAll(newData);
        fireContentsChanged(this, 0, this.data.size() - 1);
    }
    
    public void setTrackStatus(Track track, TrackStatus status)
    {
        int idx = this.data.indexOf(track);
        if (idx != -1)
        {
            track.setStatus(status);
            fireContentsChanged(this, idx, idx);
        }
    }
    
    public void appendTracks(Collection<Track> tracks)
    {
        if (tracks == null)
        {
            return;
        }

        HashSet<Track> set = new HashSet<Track>(tracks);
        set.addAll(tracks);
        
        if (!tracks.isEmpty())
        {
            int startIdx = this.data.size();
            this.data.addAll(set);
            fireIntervalAdded(this, startIdx, this.data.size() - 1);
        }
    }

    public int indexOf(Track track)
    {
        if (track != null)
        {
            return this.data.indexOf(track);
        }
        else
        {
            return -1;
        }
    }
    
    public Track nextScheduledTrack()
    {
        List<Track> clone = new ArrayList<Track>(this.data);
        for (Track track : clone)
        {
            if (track.getStatus() == TrackStatus.SCHEDULED)
            {
                return track;
            }
        }

        return null;
    }
}